﻿@page "/xlsio/encrypt-and-decrypt"
@inject Microsoft.JSInterop.IJSRuntime JS
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnvironment
@using System.IO;

@using Syncfusion.Blazor.Buttons
@*Hidden:Lines*@

@using BlazorDemos.Data.FileFormats.XlsIO

@*End:Hidden*@
@inherits SampleBaseComponent;

<SampleDescription>
   <p>This sample demonstrates how to encrypt and decrypt workbooks using XlsIO.</p> 
</SampleDescription>
<ActionDescription>
<p>This sample focuses on the encryption and decryption of an Excel 2007, Excel 2010, Excel 2013 and Excel 2016 workbook using Essential XlsIO.</p>
<p style='display: block'>Encryption is a method of protecting workbook data. It is based on a password that converts the data into a form that cannot be understood; it also restricts anonymous users from accessing a document. Decryption is the process of converting encrypted data back into its original form so that data can be read from the workbook.</p>
<p style='display: block'>A password for encrypting a workbook can be set in Microsoft Excel 2007, Excel 2010 and Excel 2013 through the Office Menu->Prepare->Encrypt Document command.</p>
<p style='display: block'><b>Note:</b> Password protection of a workbook file is different from the workbook structure and window protection you can set in the <b>Review->Protect Workbook</b></p>
<p style='display: block'><b>Features:</b></p>
<ul>
<li>Encrypting with a password</li>
<li>Decrypting with a password</li>
</ul>
</ActionDescription>

<div class="control-section">
    <p style="font-weight:normal">Click the button to view an Excel spreadsheet generated by Essential XlsIO. Please note that Microsoft Excel Viewer or Microsoft Excel is required to view the resultant document.</p>

    <div class="radio-control">
        <h5 class="row">Save As:</h5>
        <div class="e-radio-wrapper">
            <SfRadioButton Label="XLS" Name="XLS" Value=@("XLS") @bind-Checked="@excelVersion"></SfRadioButton>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <div class="e-radio-wrapper">
            <SfRadioButton Label="XLSX" Name="XLSX" Value=@("XLSX") @bind-Checked="@excelVersion"></SfRadioButton>
        </div>
    </div>
    <div class="button-section">
        <div id="button-control">
            <div class="row">
                <div>
                    <SfButton @onclick="Encrypt">Encrypt Document</SfButton>&nbsp;
                    <SfButton @onclick="Decrypt">Decrypt</SfButton>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .radio-control {
        margin: 5% 0 2% 0;
    }

    .control-section .row {
        margin: 10px 0;
    }
</style>

@code {
    MemoryStream excelStream;
    string excelVersion = "XLSX";
    string button = string.Empty;
    /// <summary>
    /// Create and download the encrypted Excel document
    /// </summary>
    public async Task Encrypt()
    {
        EncryptAndDecryptService service = new EncryptAndDecryptService(hostingEnvironment);
        button = "Encrypt Document";
        excelStream = service.EncryptAndDecryptXlsIO(button, excelVersion);
        if (excelVersion == "XLSX")
        {
            await JS.SaveAs("Sample.xlsx", excelStream.ToArray());
        }
        else
        {
            await JS.SaveAs("Sample.xls", excelStream.ToArray());
        }
    }
    /// <summary>
    /// Download the decrypted Excel document
    /// </summary>
    public async Task Decrypt()
    {
        EncryptAndDecryptService service = new EncryptAndDecryptService(hostingEnvironment);
        button = "Decrypt Document";
        excelStream = service.EncryptAndDecryptXlsIO(button, excelVersion);
        if (excelVersion == "XLSX")
        {
            await JS.SaveAs("Sample.xlsx", excelStream.ToArray());
        }
        else
        {
            await JS.SaveAs("Sample.xls", excelStream.ToArray());
        }
    }
}